package com.base.commonlib.http.utils;

import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.base.commonlib.config.MvpConstants;


/**
 * Created by jp on 2018/8/2.
 */

public class SnackBarUtil {
    public static void showMsg(View v, String msg) {
        if (v != null) {
            Snackbar make = Snackbar.make(v, msg, Snackbar.LENGTH_SHORT);
            setColor(make, Color.parseColor(MvpConstants.SNACKBAR_BG_COLOR), Color.parseColor(MvpConstants.SNACKBAR_TEXT_COLOR), 0);
            make.show();

        }

    }

    public static void ShowMsg_C(View v, String msg, int bgColor, int textColor, int actionColor) {
        if (v != null) {
            Snackbar make = Snackbar.make(v, msg, Snackbar.LENGTH_SHORT);
            setColor(make, bgColor, textColor, actionColor);
            make.show();
        }
    }

    public static void showMsg_Action(View v, String msg, String action, View.OnClickListener listener) {
        if (v != null) {
            Snackbar snackbar = Snackbar.make(v, msg, Snackbar.LENGTH_INDEFINITE).setAction(action, listener);
            setColor(snackbar, Color.parseColor(MvpConstants.SNACKBAR_BG_COLOR), Color.parseColor(MvpConstants.SNACKBAR_TEXT_COLOR), Color.parseColor(MvpConstants.SNACKBAR_ACTION_COLOR));
            snackbar.show();

        }
    }

    public static void showMsg_Action_miss(View v, String msg, String action, View.OnClickListener listener) {
        if (v != null) {
            Snackbar snackbar = Snackbar.make(v, msg, Snackbar.LENGTH_LONG).setAction(action, listener);
            setColor(snackbar, Color.parseColor(MvpConstants.SNACKBAR_BG_COLOR), Color.parseColor(MvpConstants.SNACKBAR_TEXT_COLOR), Color.parseColor(MvpConstants.SNACKBAR_ACTION_COLOR));
            snackbar.show();

        }
    }


    public static void showMsg_Action_C(View v, String msg, String action, int bgColor, int textColor, int actionColor, View.OnClickListener listener) {
        if (v != null) {
            Snackbar snackbar = Snackbar.make(v, msg, Snackbar.LENGTH_INDEFINITE).setAction(action, listener);
            setColor(snackbar, bgColor, textColor, actionColor);
            snackbar.show();

        }
    }


    private static void setColor(Snackbar snackbar, int bgColor, int textColor, int actionColor) {
        try {
            View view = snackbar.getView();
            if (bgColor != 0) {

                view.setBackgroundColor(bgColor);
            }
            if (textColor != 0) {
                TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
                tv.setTextColor(textColor);
            }
            if (actionColor != 0) {
                TextView tv_action = view.findViewById(android.support.design.R.id.snackbar_action);
                tv_action.setTextColor(actionColor);
            }

        } catch (Exception e) {
            Log.i("", "");
        }

    }
}
